package com.aischool.webapp.family;

import java.util.List;

import com.aischool.common.model.AiParent;
import com.aischool.common.model.SysUser;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiClassService;
import com.aischool.service.AiKindredService;
import com.aischool.service.AiParentService;
import com.aischool.service.AiStudentService;
import com.aischool.service.FileUploadService;
import com.aischool.service.SysUserService;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/front/family/parent")
public class ParentController extends BaseFamilyController {
	
	@Inject
	AiParentService aiParentService;
	@Inject
	AiClassService aiClassService;
	@Inject
	AiStudentService aiStudentService;
	@Inject
	AiKindredService aiKindredService;
	@Inject
	FileUploadService fileUploadService;
	@Inject
	SysUserService sysUserService;

	public void index() {
		render(view_path + "/parent-index.html");
	}
	/**
	 * 个人资料
	 */
	public void myinfo() {
		
		int login_org_id = this.getAttrForInt("login_org_id");
		
		int class_id = this.getAttr("login_class_id", 0);

		
		// 获取家长资料 Inject已有
		// 获取亲属
		List<Record> listKindred = aiKindredService.getAll();
		// 获取班级
		List<Record> listClass = aiClassService.getAll(login_org_id, 1);
		if (class_id <= 0 && listClass != null && listClass.size() > 0) {
			class_id = listClass.get(0).getInt("class_id");
		}
		if (class_id>0) {
			List<Record> listStudents = aiStudentService.getStudents(class_id);
			this.setAttr("listStudents", listStudents);
		}
		
		this.setAttr("listKindred", listKindred);
		this.setAttr("listClass", listClass);
		

		render(view_path + "/myinfo.html");
	}
	
	public void myset() {
		render(view_path + "/myset.html");
	}
	
	public void mybaby() {
		
		int stu_id = getInt("stuid",0);
		
		// 获取家长资料 Inject已有
		// 获取学生资料
		if (stu_id>0) {
			Object obj = aiStudentService.getOne(stu_id);
			this.setAttr("student_rd", obj);
		}
		

		render(view_path + "/mybaby.html");
	}
	
	public void upload_userhead() {
		
		this.setAttr("filemodel", "base64");
		
		this.setAttr("zoom_type", "wh");
		this.setAttr("zoom_w", 100);
		this.setAttr("zoom_h", 100);
		
		JsonResult jr = fileUploadService.uploadFile(this);
		if (jr.getCode() == 0) {
			int login_user_id = this.getAttrForInt("login_user_id");
			SysUser info = new SysUser();
			info.setUserImg(jr.getDicKey("fileurl"));
			info.setUserId(login_user_id);
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			boolean bf = info.update();
			if (bf) {
				sysUserService.removeCache(login_user_id);
			}else {
				String msg = jr.getMsg();
				jr.setEnum(JsonCodeEnum.Faild);
				jr.appendMsg(msg);
			}
		}
		renderJson(jr.Dic);
	}
	
	public void update() {
		JsonResult jr =new JsonResult();
		
		int login_user_id = this.getAttrForInt("login_user_id");
		int login_parent_id = this.getAttrForInt("login_parent_id");
		//int login_org_id = this.getAttrForInt("login_org_id");
		
		int kindred_id = getInt("kindred_id", 0);
		int class_id = getInt("class_id", 0);
		String parent_name = get("parent_name", "");

		int student_id = getInt("student_id", 0);
		
		AiParent info = new AiParent();
		boolean is_update = false;
		
		if (kindred_id>0) {
			info.setKindredId(kindred_id);
			is_update = true;
		}
		if (class_id>0) {
			info.setClassId(class_id);
			is_update = true;
			
			List<Record> listStudents = aiStudentService.getStudents(class_id);
			jr.setDicKV("listStudents", listStudents);
		}
		if (parent_name.length()>0) {
			info.setParentName(parent_name);
			is_update = true;
		}
		if (student_id>0) {
			info.setStudentId(student_id);
			is_update = true;
		}
		
		if (is_update) {
			info.setParentId(login_parent_id);
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			boolean bf = info.update();
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
				aiParentService.removeCacheByUserId(login_user_id);
			}else {
				String msg = jr.getMsg();
				jr.setEnum(JsonCodeEnum.Faild);
				jr.appendMsg(msg);
			}
		}else {
			if (StrKit.isBlank(jr.getMsg())) {
				jr.appendMsg("没有发现要更改的数据！");
			}
		}
		
		renderJson(jr.Dic);
	}
}
